from common.cache import configure_cache
from model.model import Configure


class ConfigureView(Configure):
    @classmethod
    def update_configure(cls, session, name, update):
        """修改配置 并设置缓存"""
        item = cls.update_item(session, filter_=[cls.name == name], update=update)
        configure_cache.set(name, {'name': name, **update})
        return item

    @classmethod
    def load_database2cache(cls, session):
        items = cls.get_items(session)
        for item in items:
            configure_cache.set(item.name, {'name': item.name, 'config': item.config, 'description': item.description})
